package com.spark3;

import org.apache.commons.lang3.reflect.FieldUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

public class CustomerClassLoader {

    public static void main(String[] args) throws Exception{

       LocalClassLoader lcl = new LocalClassLoader();
       Class<?> cls = lcl.loadClass("com.spark3");
       Field field = FieldUtils.getField(cls,"key",true);
       Object value = field.get(cls.newInstance());
       System.out.println(value);

    }
}

class LocalClassLoader extends ClassLoader{

    private String path = "/Users/tanweihua/Desktop/KeyDemo";

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        Class<?> cls = findLoadedClass(name);
        if (cls != null){
            return cls;
        }

        if (!name.endsWith(".KeyDemo")){
            return super.loadClass(name);
        }

        try {
            InputStream is = new FileInputStream(path + name.replace(".","/") + ".class");

        } catch (IOException e){
            e.printStackTrace();
        }


        return super.findClass(name);
    }
}